// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/dwio/common/measure_time.h>

using namespace ::testing;
using namespace ::kumo::pollux::dwio::common;

TEST(MeasureTimeTests, DoesntCreateMeasureIfNoCallback) {
  EXPECT_FALSE(measureTimeIfCallback(nullptr).has_value());
}

TEST(MeasureTimeTests, CreatesMeasureIfCallback) {
  auto callback =
      std::function<void(std::chrono::high_resolution_clock::duration)>(
          [](const auto&) {});
  EXPECT_TRUE(measureTimeIfCallback(callback).has_value());
}

TEST(MeasureTimeTests, MeasuresTime) {
  bool measured{false};
  {
    auto callback =
        std::function<void(std::chrono::high_resolution_clock::duration)>(
            [&measured](const auto&) { measured = true; });
    auto measure = measureTimeIfCallback(callback);
    EXPECT_TRUE(measure.has_value());
    EXPECT_FALSE(measured);
  }
  EXPECT_TRUE(measured);
}
